package com.aoxin.service.impl.opm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.opm.SlfRoleMapper;
import com.aoxin.mapper.opm.SlfRoleMenuMapper;
import com.aoxin.model.opm.SlfRole;
import com.aoxin.service.inter.opm.RoleService;
import com.aoxin.util.ErrorUtil;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private SlfRoleMapper slfRoleMapper;

	@Autowired
	private SlfRoleMenuMapper slfRoleMenuMapper;

	@Override
	@Transactional
	public void insertSlfRole(SlfRole role) {
		Assert.notNull(role, String.format(ErrorUtil.PARAMETER_NULL, "role"));
		if (null == role.getSequence()) {
			role.setSequence(this.slfRoleMapper.getNextSequence().intValue());
		}
		if (this.slfRoleMapper.checkName(role.getName(), null) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, role.getName()));
		}
		this.slfRoleMapper.insertSelective(role);
	}

	@Override
	@Transactional
	public void deleteSlfRoleByIds(List<Integer> roleIds) {
		Assert.notEmpty(roleIds, String.format(ErrorUtil.PARAMETER_NULL, "roleIds"));
		Map<String, Object> map = new HashMap<String, Object>();
		for (Integer integer : roleIds) {
			map.put("roleId", integer);
			if (this.slfRoleMenuMapper.IsRole(integer) > 0) {
				throw new IllegalArgumentException(ErrorUtil.ROLEMENU);
			}
			if (this.slfRoleMenuMapper.getRecordCount(map) > 0L) {
				throw new IllegalArgumentException(ErrorUtil.ROLE_MENU);
			}
		}
		this.slfRoleMapper.deleteByPrimaryKeys(roleIds);
	}

	@Override
	@Transactional
	public void updateSlfRole(SlfRole role) {
		Assert.notNull(role, String.format(ErrorUtil.PARAMETER_NULL, "role"));
		if (this.slfRoleMapper.checkName(role.getName(), role.getRoleId()) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, role.getName()));
		}
		this.slfRoleMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	@Transactional
	public void updateSlfRoleStates(List<Integer> roleIds, Integer state) {
		Assert.notEmpty(roleIds, String.format(ErrorUtil.PARAMETER_NULL, "roleIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		//权限组无效 有没人员
		if(state==0){
			for (int i = 0,b=roleIds.size(); i < b; i++) {
				if("true".equals(slfRoleMapper.queryRoleExistenceEmployee(roleIds.get(i)))){
					throw new IllegalArgumentException("不允许禁用已分配给员工的角色");
				}
				Integer a = slfRoleMapper.IsRole(roleIds.get(i));
				if(a > 0) {
					throw new IllegalArgumentException("该角色已分配员工,无法禁用");
				}
			}
		}
		this.slfRoleMapper.updateStates(roleIds, state);
	}

	@Override
	public SlfRole getSlfRole(Integer roleId) {
		Assert.notNull(roleId, String.format(ErrorUtil.PARAMETER_NULL, "roleId"));
		return this.slfRoleMapper.selectByPrimaryKey(roleId);
	}

	@Override
	public List<SlfRole> getSlfRoleList(Map<String, Object> params) {
		if(params.get("state")==null || "".equals(params.get("state"))) {
			params.put("state", 1);
		}
		return this.slfRoleMapper.getList(params);
	}

	@Override
	public Long getNextSequence() {
		return this.slfRoleMapper.getNextSequence();
	}

	@Override
	public List<String> getRoleName() {
		return this.slfRoleMapper.getRoleName();
	}

}
